<?php
	 # changed tdNum and its setter/getter to tdID; added var $idle + setter/getter -- 14 Aug 2003
	 # removed strDueDate in update and used due date; removed strtotime validation in create -- 15 Aug 2003
	 # added updatePaidTaxes function which will be used to update dues(paid sef/basic/penalty/etc) -- 18 Aug 2003
	 # added select and modified updatePaidTaxes; added getBalance---, credit--- for idle and pd1185; modified getTotalDue -- 19 Aug 2003
     require_once("collection/StorableObject.php");
     require_once("collection/masterTables.php");
     class Dues{
         /** The $dueID attribute. This attribute is the identifier of the object
          ** in the database.
          ** @type identifier
          **/
         var $dueID;
         /** The $basic attribute. This attribute is the basic tax due from the
          ** specific TD.
          ** @type currency
          **/
         var $basic;
         /** The $penalty attribute. This attribute is the penalties due from
          ** non-payment of tax dues on or before the due date.
          ** @type currency
          **/
         var $penalty;
         /** The $sef attribute. This attribute is the Special Education Fund to
          ** collected as an additional surcharge for every real property tax payment.
          ** @type curreny
          **/
         var $sef;
         /** The $idle attribute. This attribute is collected as an additional
          ** surcharge for idle lands.
          ** @type currency
          **/
		 var $idle;
		 var $pd1185;
		 /** The $tdNum attribute. This attribute is the number of the specific
          ** tax declaration (TD) that tax is due.
          ** @type identifier
          **/
		 var $tdID;
         /** The $dueDate attribute. This attribute is the specified date where
          ** the basic tax must be collected or penalties will be applied.
          ** This is stored as a Unix Timestamp.
          ** Note: The valid range of a timestamp is typically from
          ** Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT.
          ** (These are the dates that correspond to the minimum and maximum values
          ** for a 32-bit signed integer.)
          ** @type date
          **/
         var $dueDate;
         /** The $updateDate attribute. This attribute is the last date of reckoning
          ** or update when the values of the dues are computed and valid.
          ** This is stored as a Unix Timestamp.
          ** Note: The valid range of a timestamp is typically from
          ** Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT.
          ** (These are the dates that correspond to the minimum and maximum values
          ** for a 32-bit signed integer.)
          ** @type date
          **/
         var $updateDate;
         /** The $paidBasic attribute. This attribute holds the payments
          ** for basic dues.
          ** @type currency
          **/
         var $paidBasic = 0;
         /** The $paidSEF attribute. This attribute holds the payments
          ** for SEF dues.
          ** @type currency
          **/
         var $paidSEF = 0;
         /** The $paidPenalty. This attribute holds the payments
          ** for Penalties incurred.
          ** @type currency
          **/
         var $paidPenalty = 0;
		 /** The $paidIdle. This attribute holds the payments
          ** for idle land dues.
          ** @type currency
          **/
		 var $paidIdle = 0;
		 /** The $paidPd1185. 
		  ** @type currency
          **/
		 var $paidPd1185 = 0;
         /** The paymentMode attribute. This attribute determines whether this
          ** due is being paid on quarterly or annual basis.
          ** @private
          ** @type string (annual, quarterly)
          **/
         var $paymentMode;
         /** The quarterspaid attribute. This attribute tells how many quarters
          ** have been paid. Assuming that you have to pay the previous quarter
          ** before you can pay the next.
          ** @private
          ** @type integer
          **/
         var $paidQuarters;
         /** The MONTH_OFFSET is a constant that determines the starting month
          ** for penalties. Set to "0" for End of Jan and "1" for Start of Jan
          **
          **/

         var $MONTH_OFFSET = 0;

		 var $amnesty;
		 var $idleStatus;

		 # for testing only
		 function getIdleStatus(){
		 	return rand(0,1); 
		 }
         
         /** The Dues method. This method is the constructor
          ** @public
          ** @returns void
          **/
		 function Dues(){
             // set value for attributes for storage to avoid errors in
             //   blank assignments.
             $this->dueID = 0;
             $this->basic = 0.00;
             $this->sef = 0.00;
             $this->penalty = 0.00;
             $this->paidBasic = 0.00;
             $this->paidSEF = 0.00;
             $this->paidPenalty = 0.00;
             $this->paidQuarters = 0;
             $this->paymentMode = "Annual";
             $this->dueDate = 0;    
			 $this->amnesty = "No";
         }
         
         function make_node($parent,$name,$content)
         {
          # adds a new child node to parent node
          $parent->new_child($name,$content);

          # return the newly added child as a reference
          return $parent->lastchild();
         }
         function setDocNode(){
             $dueDOM = domxml_new_doc("1.0");
             $root = $dueDOM->create_element("dues");
             $root = $dueDOM->append_child($root);
             $node = $dueDOM->create_element("due");
             $dueNode = $root->append_child($node);
             foreach((array) $this as $attributeName => $value){
              $varType = gettype($value);
              #echo "$attributeName has $value with type $varType<br>\n";
              if ($attributeName != "")
              switch($varType){
                  case 'array':
                       #echo " make an array<br>\n";
                       $node = $dueDOM->create_element($attributeName);
                       $node->set_attribute('attrType',$varType);
                       foreach($value as $key => $arrValue){
                           $arrVarType = gettype($arrValue);
                           $arrNode = $dueDOM->create_element("LUT");
                           $arrNode->set_attribute('attrType',$arrVarType);
                           $arrNode->set_attribute('attrKey',$key);
                           $arrNode->set_content($arrValue);
                           $node->append_child($arrNode);
                       }
                       $dueNode->append_child($node);
                       break;
                  default:
                       #echo " make an $varType<br>\n";
                       $node = $dueDOM->create_element($attributeName);
                       $node->set_content($value);
                       $node->set_attribute('attrType',$varType);
                       $dueNode->append_child($node);
                       break;
              }
             }
             return $dueDOM->dump_mem(true);
         }
         # assumes an XML document with a due root node
         function parseDOMDocument($xmlString){
             $dueDOM = domxml_open_mem($xmlString);
             $root = $dueDOM->document_element();
             $children = $root->child_nodes();
             # shift through the attributes
             foreach($children as $node){
                 $attrType = $node->get_attribute('attrType');
                 echo "parsing an $attrType";
                 switch($attrType){
                     case '':
                          break;
                     case 'array':
                          $attrName = $node->node_name();
                          echo " named $attrName <br>\n";
                          $entries = $node->child_nodes();
                          foreach($entries as $entry){
                              $key = $entry->get_attribute('key');
                              $entryType = $entry->get_attribute('attrType');
                              $entryValue = $entry->get_content();
                              settype($entryValue,$entryType);
                              $entryArray[$key]=$entryValue;
                          }
                          $varname = "\$this->".$attrName;
                          $$varname=$entryArray;
                          break;
                     default:
                          $attrName = $node->node_name();
                          $attrValue = $node->get_content();
                          echo " named $attrName and value $attrValue<br>\n";
                          settype($attValue,$attrType);

                          $varname = '$this->'.$attrName;
                          echo "will save to $varname<br>\n";
                          $$varname=$attrValue;
                          break;
                 }
             }
         }

         function setAmnesty($value){
              $this->amnesty = $value;
         }
         function getAmnesty(){
              return $this->amnesty;
         }

         function setDueID($identifier){
              $this->dueID = $identifier;
         }
         function getDueID(){
              return $this->dueID;
         }
         /** The SetBasic method. This method sets the Basic Dues for the TaxDec
          ** The Basic due is a fixed value once set along with the due date.
          ** Further accounting will be reflected in the $basicBalance attribute.
          ** Should also set the $basicBalance once.
          ** @public
          ** @returns void
          **/
         function setBasic($currencyValue){
             global $pctRPTax;
             if($currencyValue > 0.00 && $this->basic == 0.0){
                 # compute for basic using the $currencyValue * tax rate;
                 $this->basic = $currencyValue * $pctRPTax;
             }
         }
         /** The getBasic method. This method returns basic tax due.
          ** if you want the current basic tax due, then use the getBasicBalance
          ** method.
          ** @public
          ** @returns double
          **/
         function getBasic($period = "Annual"){
             switch($period){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      return $this->basic/4;
                      break;
                 case 'Annual':
                 default:
                      return $this->basic;
                      break;
             }
         }
         /** The setPenalty method. This method sets the penalty for this tax due.
          ** Penalties should be computed based on basic due and due date.
          ** @public
          ** @returns double
          **/
         function setPenalty($penalty){
             $this->penalty = $penalty;
         }        
         /** The getPenalty method. This method returns the total computed penalties
          ** based on the last updateDate.
          ** @public
          ** @returns object
          **/
         function getPenalty(){
             return $this->penalty;
         }
		 /** The setSEF method. This method sets the SEF due for this TD collection.
          ** SEF is computed based on basic due.
          ** @public
          ** @returns void
          **/
         function setSEF($currencyValue){
             global $pctSEF;
             if($currencyValue > 0.00 && $this->sef == 0.0){
                 # compute for basic using the $currencyValue * tax rate;
                 $this->sef = $currencyValue * $pctSEF;
             }
         }
         /** The getSEF method. This method returns total SEF for this tax Due.
          ** If you want the remaining SEF due after payments use getBalanceSEF.
          ** @public
          ** @returns double
          **/
         function getSEF($period = "Annual"){
             switch($period){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      return $this->sef/4;
                      break;
                 case 'Annual':
                 default:
                      return $this->sef;
                      break;
             }
         }
		 function setIdle($currencyValue){
		 	global $pctIdle;
             if($currencyValue > 0.00 && $this->idle == 0.0){
                 # compute for basic using the $currencyValue * tax rate;
                 $this->idle = $currencyValue * $pctIdle;
             }
		 }
		 function getIdle($period = "Annual"){
		      switch($period){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      return $this->idle/4;
                      break;
                 case 'Annual':
                 default:
                      return $this->idle;
                      break;
             }
		 }
		 function setPd1185($currencyValue){
			  $this->pd1185 = $currencyValue;
		 }
		 function getPd1185($period = "Annual"){
		      switch($period){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      return $this->idle/4;
                      break;
                 case 'Annual':
                 default:
                      return $this->idle;
                      break;
             }
		 }
         /** The computePenalty method. This method returns the total computed penalties
          ** based on the specific quarter or annual payment and the payDate.
          ** @public
          ** @returns object
          **/
         function computePenalty($period, $payDate="now"){
             global $penaltyLUT, $monthOffset;
			 # used mktime(timestamp format) instead of date format from $this->dueDate
             list($year, $month, $day) = explode("-",$this->dueDate);
			 # convert determine the amount of time transpired since due date
             switch($period){
                 case "Q1":
                      $startTime = strtotime("+3 months -1 day",mktime(0,0,0,$month,$day,$year));
                      break;
                 case "Q2":
                      $startTime = strtotime("+6 months -1 day",mktime(0,0,0,$month,$day,$year));
                      break;
                 case "Q3":
                      $startTime = strtotime("+9 months -1 day",mktime(0,0,0,$month,$day,$year));
                      break;
                 case "Q4":
                      $startTime = strtotime("+12 months -1 day",mktime(0,0,0,$month,$day,$year));
                      break;
                 case "Annual":
                 default:
                     $startTime = mktime(0,0,0,$month,$day,$year);
                     break;
             }
			 $reckonDate = date("M-j-Y",$startTime);
             $startDate = getdate($startTime);
			 $endDate = getdate(strtotime($payDate));
             # count the years
             $numYears = $endDate['year'] - $startDate['year'];
             # count the months
             $numMonths = $endDate['mon'] - $startDate['mon'];
             $totalMonths = ($numYears*12) + $numMonths;
             $totalMonths = ($totalMonths<36) ? $totalMonths : 36;
             $pctPenalty =  $penaltyLUT[$totalMonths];
             return $pctPenalty;
         }

		 /** The resetPenalty method. This method returns penalty to zero.
          ** Is reset when amnesty is declared.
          ** @public
          ** @returns void
          **/
		 function resetPenalty(){
		 	$this->penalty = 0;
		 }
         /** The setTDNum method. This method assigns the specific TDNum for which
          ** tax is due. It should be set first before any other value can be
          ** computed.
          ** @public
          ** @returns void
          **/
         function setTdID($identifier){
             $this->tdID = $identifier;
         }
         /** The getTDNum method. This method returns the TDNumber for this tax due.
          ** @public
          ** @returns identifier
          **/
         function getTdID(){
             return $this->tdID;
         }
         /** The setDueDate method. This method sets the due date for this tax.
          ** It is set only once and will not be changed again.
          ** @public
          ** @returns void
          **/
         function setDueDate($dateString){
             /*if($this->dueDate > 0){
                 return true;
             }
             $validDate = strtotime($dateString);
             if($validDate > -1){
                 $this->dueDate=$validDate;
                 return true;
             }
             else {
                 return false;
             }*/
			 $this->dueDate = $dateString;
         }
         /** The getDueDate method. This method returns the due date for the basic
          ** taxes.
          ** @public
          ** @returns date
          **/
         function getDueDate(){
             return $this->dueDate;
			 # return date("Y-m-d",$this->dueDate);
         }
         /** The setupdateDate method. This method sets the date of validity of the
          ** balance values (balanceBasic, balanceSEF, balancePenalties).
          ** Maybe it should also update the three balances everytime.
          ** @public
          ** @returns void
          **/
         function setUpdateDate(){
             $this->updateDate=time();
         }
         /** The getUpDate method. This method returns the current date of last computation.
          ** @public
          ** @returns date
          **/
         function getUpdateDate(){
             return $this->updateDate;
         }
         /** The setPaidBasic method. This method sets the paidBasic.
          ** this is only here for compliance, you should use creditBasic to
          ** update the $paidBasic attribute
          ** @public
          ** @returns void
          **/
         function setPaidBasic(){
         }
         /** The getPaidBasic method. Returns the amount paid for basic dues.
          ** @public
          ** @returns currency
          **/
         function getPaidBasic(){
             return $this->paidBasic;
         }
         /** The setPaidSEF method. This method sets the paidSEF attribute.
          ** this is only here for compliance, you should use creditBasic to
          ** update the $paidBasic attribute
          ** @public
          ** @returns void
          **/
         function setPaidSEF($currencyValue){
         }
         /** The getPaidSEF method. This method returns the amount paid for SEF
          ** so far.
          ** @public
          ** @returns currency
          **/
         function getPaidSEF(){
             return $this->paidSEF;
         }
         /** The setpaidPenalty method. This method sets the paid of penalties.
          ** this is only here for compliance, you should use creditBasic to
          ** update the $paidBasic attribute
          ** @public
          ** @returns void
          **/
         function setPaidPenalty(){
         }
         /** The getPaidPenalty method. This method will return the amount paid
          ** so far for penalties.
          ** @public
          ** @returns currency
          **/
         function getPaidPenalty(){
             return $this->paidPenalty;
         }
		 function setPaidIdle(){
		 }
		 function getPaidIdle(){
		 	return $this->paidIdle;
		 }
		 function setPd1185(){
		 }
		 function getPd1185(){
		 	return $this->pd1185;
		 }
         /** The getTotalDue method. This method returns the sum of the balances
          ** balanceBasic + balancePenalty + balanceSEF.
          ** @public
          ** @returns double
          **/
         function getTotalDue(){
             $totalDue = $this->getBalanceBasic() + $this->getBalanceSEF() + $this->getBalanceIdle() + $this->getBalancePd1185() + $this->getBalancePenalty();
             return $totalDue;
         }
         /** The getBalanceBasic method. This method returns the remaining basic tax due.
          ** @public
          ** @returns double
          **/
         function getBalanceBasic(){
			switch($this->$paymentMode){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      $tempBasic = $this->basic/4;
                      break;
                 case "Annual":
                 default:
                      $tempBasic =  $this->basic;
                      break;
             }

             $balance = $tempBasic - $this->paidBasic;
             return $balance;
         }
         /** The getBalanceSEF method. This method returns the balance of SEF dues.
          ** @public
          ** @returns double
          **/
         function getBalanceSEF(){
             switch($this->paymentMode){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      $tempSEF = $this->sef/4;
                      break;
                 case 'Annual':
                 default:
                      $tempSEF = $this->sef;
                      break;
             }
			 $balance = $tempSEF - $this->paidSEF;
             return $balance;
         }

         /** The getBalancePenalty method. This method returns the balance of penalties
          ** @public
          ** @returns object
          **/
         function getBalancePenalty(){
		 	 # $balance = ($this->penalty*($this->basic + $this->sef + $this->idle)) - $this->paidPenalty;
             $balance = $this->penalty - $this->paidPenalty;
             return $balance;
         }
		 
		 function getBalanceIdle(){
		 	 switch($this->paymentMode){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      $tempIdle = $this->idle/4;
                      break;
                 case 'Annual':
                 default:
                      $tempIdle = $this->idle;
                      break;
             }
             $balance = $this->idle - $this->paidIdle;
             return $balance;
         }
		 
		 function getBalancePd1185(){
		 	 switch($this->paymentMode){
                 case "Q1":
                 case "Q2":
                 case "Q3":
                 case "Q4":
                      $tempPd1185 = $this->pd1185/4;
                      break;
                 case 'Annual':
                 default:
                      $tempPd1185 = $this->pd1185;
                      break;
             }
             $balance = $tempPd1185 - $this->paidPd1185;
             return $balance;
         }
         /** The creditBasic method. This method credits the balanceBasic indicating payment of dues.
          ** returns the new balanceBasic.
          ** @public
          ** @returns double
          **/
         function creditBasic($currencyValue){
             if($currencyValue > 0.00){
			 	 echo("<br>paid basic before ".$this->paidBasic."<br>");
				 echo("value added ".$currencyValue."<br>");
                 $this->paidBasic += $currencyValue;
				 echo("paid basic after ".$this->paidBasic."<br>");
             }

         }
         /** The creditSEF method. This method credits the balanceSEF indicating the payment of dues
          ** returns the new balanceSEF
          ** @public
          ** @returns double
          **/
         function creditSEF($currencyValue){
             if($currencyValue > 0.00){
                 $this->paidSEF += $currencyValue;
             }
         }
         /** The creditPenalty method. This method credits the balancePenalty indicating the payment of dues
          ** returns the new balancePenalty
          ** @public
          ** @returns double
          **/
         function creditPenalty($currencyValue){
             if($currencyValue > 0.00){
                 # $this->paidPenalty += $currencyValue;
				 $this->paidPenalty = $currencyValue;
             }
         }
		 function creditIdle($currencyValue){
             if($currencyValue > 0.00){
                 $this->paidIdle += $currencyValue;
             }
         }
		 function creditPd1185($currencyValue){
             if($currencyValue > 0.00){
                 $this->paidPd1185 += $currencyValue;
             }
         }
         /** The recomputePayments method. This method will reset the entire object and reapply payments
          ** to update the balances (basicBalance, sefBalance, penaltyBalance).
          ** returns the totalBalance();
          ** @public
          ** @returns double
          **/
         function recomputePayments(){
         }
         /** The setPaymentMode method. This method will set the $paymentMode value.
          ** @public
          ** @returns void
          **/
         function setPaymentMode($stringValue = "Annual"){
             if($stringValue == "Annual") {
                 $this->paymentMode = "Annual";
             }
             else {
                 $this->paymentMode = "Quarterly";
             }
         }
         /** The getPaymentMode method. This method will get the $paymentMode value.
          ** @public
          ** @returns string
          **/
         function getPaymentMode(){
             return $this->paymentMode;
         }
         /** The countMonths method. This method will count the number of complete
          ** months that have transpired between the due date and the current date
          ** @private
          ** @returns integer
          **/
         function countMonths(){
             // Start off with 0
             $count = 0;
             // Get the current year and month
			 # changed getdate to date to be able to subtract values
             list($year, $month, $day) = explode("-",date("Y-m-d"));
             // Get the due date year and month
             list($dueYear, $dueMonth, $dueDay) = explode("-",$this->dueDate);
             // Count the number of years transpired and
             // multiply by 12 months. (same year = 0 months)
             if($year > $dueYear){
                 $count = ($year - $dueYear) * 12;
             }
             // Add the differnce between the due month and the current month
             // Due month is always Jan, Current is always >= Due month
             $count += ($month-$dueMonth);
             // Then finally, add the month offset;
             $count += $MONTH_OFFSET;
             return $count;
         }
         function store(){
               //if isStoredInDatabase then use an update statement
               if($this->isStoredInDatabase){
			   	   return $this->update();
               }
               else{// else use an insert statement
                    //create a DB object
                    $rptsDB = new DB_RPTS;
                    //prepare SQL statement
                    $strUpDate = date("Y-m-d H:i:s",time()); // changed from $this->updateDate
                    # $strDueDate = date("Y-m-d",$this->dueDate);
                    $sqlinsert = "INSERT INTO dues
                         SET basic   = '$this->basic',
                             penalty = '$this->penalty',
                             sef     = '$this->sef',
                             idle     = '$this->idle',							 
                             tdID   = '$this->tdID',
                             dueDate = '$this->dueDate',
                             currentDate  = '$strUpDate', 
							 paidBasic    = '$this->paidBasic',
                             paidSEF      = '$this->paidSEF',
                             paidPenalty  = '$this->paidPenalty',
							 paidIdle  = '$this->paidIdle',
                             paidQuarters = '$this->paidQuarters',
                             paymentMode  = '$this->paymentMode'";
                    // query the database
					$queryID = $rptsDB->query($sqlinsert);
                    if($queryID){
                        $this->dueID = $rptsDB->insert_id();
                        $this->isStoredInDatabase = true;
                        return true;
                    }
                    else{
                        return false;
                    }
               }
         }
		 function select(){
		 	   echo("selecting record<br>");
		 	   $rptsDB = new DB_RPTS;
			   $sqlselect = "SELECT * FROM dues WHERE dueID = '$this->dueID'";
			   $queryID = $rptsDB->query($sqlselect);
			   if($queryID){
			   	   if($rptsDB->next_record()){
					   $this->basic        = $rptsDB->f("basic");
					   $this->penalty      = $rptsDB->f("penalty");
					   $this->sef          = $rptsDB->f("sef");
					   $this->idle         = $rptsDB->f("idle");
					   $this->tdID         = $rptsDB->f("tdID");
					   $this->dueDate      = $rptsDB->f("dueDate");
					   $this->updateDate   = $rptsDB->f("currentDate");
					   $this->paidBasic    = $rptsDB->f("paidBasic");
					   $this->paidPenalty  = $rptsDB->f("paidPenalty");
					   $this->paidSEF      = $rptsDB->f("paidSEF");
					   $this->paidIdle     = $rptsDB->f("paidIdle");				 
					   $this->paymentMode  = $rptsDB->f("paymentMode");
					   $this->paidQuarters = $rptsDB->f("paidQuarters");
			  	   }
				   return true;
			  }else{
			       return false;
			  }
		 } 
         function delete(){
               //create a db object
               $rptsDB = new DB_RPTS;
               //prepare an SQL delete statement
               $sqldelete = "DELETE from DUES where dueID = '$this->dueID'";
               //query the database
               $queryID = $rptsDB->query($sqldelete);
               if($queryID){
                   $this->isStoredInDatabase = false;
                   return true;
               }
               else{
                   return false;
               }
         }
         function update(){
		 		echo("updating record<br>");
               //create a DB object
               $rptsDB = new DB_RPTS;
               // prepare an SQL update statement
               // programming option: it is decided to use direct access to attributes
               //                     when saving and creating the object from database
			   $strUpDate = date("Y-m-d H:i:s",time()); // changed from $this->updateDate
               # $strDueDate = date("Y-m-d",$this->dueDate);
			    $sqlupdate = sprintf("update %s set ".
						"basic = '%s'".
						", penalty = '%s'".
						", sef = '%s'".
						", idle = '%s'".
						", tdID = '%s'".
						", dueDate = '%s'".
						", currentDate = '%s'".
						", paidBasic = '%s'".
						", paidSEF = '%s'".
						", paidPenalty = '%s'".
						", paidIdle = '%s'".
						", paidQuarters = '%s'".
						", paymentMode = '%s'".
						", amnesty = '%s'".
						" where dueID = %s"
						, dues
						, fixQuotes($this->basic)
						, fixQuotes($this->penalty)
						, fixQuotes($this->sef)
						, fixQuotes($this->idle)
						, fixQuotes($this->tdID)
						, fixQuotes($this->dueDate)
						, fixQuotes($strUpDate)
						, fixQuotes($this->paidBasic)
						, fixQuotes($this->paidSEF)
						, fixQuotes($this->paidPenalty)
						, fixQuotes($this->paidIdle)
						, fixQuotes($this->paidQuarters)
						, fixQuotes($this->paymentMode)
						, $this->amnesty
						, $this->dueID
					);
               //query the database
               $queryID = $rptsDB->query($sqlupdate);
               if($queryID){
                   return true;
               }
               else{
                   return false;
               }
         }
		 
		 function updatePaidTaxes($collectionID){
	         $rptsDB = new DB_RPTS;
			 # get payments belonging to a collection
			 $sqlselect = "SELECT payments.dueID, payments.dueType, payments.amount FROM collectionPayments ".
						  "INNER JOIN payments ON payments.paymentID = collectionPayments.paymentID ".
						  "WHERE collectionID = '$collectionID'";
          	 $queryID = $rptsDB->query($sqlselect);
			 $numRows = mysql_num_rows($queryID);

          	 if($queryID){
			    # get amount paid and payment type from each payment and use this to update paid taxes in dues table
				$flag = 0;
				$tempBasic = 0;
				$tempSEF = 0;
				$tempIdle = 0;
				$tempPd1185 = 0;
				$tempPenalty = 0;
							
				while($rptsDB->next_record()) {
					$ctr++;
					echo("$ctr<br>");
					$dueType = $rptsDB->f('dueType');
					$dueID = $rptsDB->f('dueID');
					# if dueID is not equal to flag, then already going through next dueID; save data of previous dueID 
					# and reset all temp due types; ** NO fields in dues table for PD1185 and paidPD1185 yet.
					if(($dueID != $flag) && ($flag != 0) || ($numRows == $ctr)){
						echo("<br>updating dues<br>");
						$tempDues = new Dues();
						$tempDues->setDueID($flag);
						echo("after setting dueID<br>");
						print_r($tempDues);
						echo("<br><br>");
						$tempDues->select();
						print_r($tempDues);
						$tempDues->creditBasic($tempBasic);
						$tempDues->creditSEF($tempSEF);
						$tempDues->creditIdle($tempIdle);
						$tempDues->creditPd1185($tempPd1185);
						$tempDues->creditPenalty($tempPenalty);
						echo("<br>after crediting values<br><br>");
						print_r($tempDues);
						$tempDues->update();
						# reset temp vars
						$tempBasic = 0;
						$tempSEF = 0;
						$tempIdle = 0;
						$tempPd1185 = 0;
						$tempPenalty = 0;
					}

					switch($dueType){
						case 'basic':
							$tempBasic = $rptsDB->f("amount");
							break;
						case 'sef':
							$tempSEF = $rptsDB->f("amount");
							break;
						case 'idle':
							$tempIdle = $rptsDB->f("amount");
							break; 
						case 'pd1185':
							$tempPd1185 = $rptsDB->f("amount");
							break;
						case 'penalty':
							$tempPenalty = $rptsDB->f("amount");
							break;
						default:
							break;
					}# end of switch
					$flag = $dueID;
					$dueType = "";
					$dueID = 0; 
				}# end of while 
          	 }
          	 else {
              	return false;
          	 }		 
		 }
		                     // changed from $rptopDate     
         function create($tdID, $dateDue){
               //create a DB object
               $rptsDB = new DB_RPTS;
			   $this->dueDate = $dateDue;
               //prepare an SQL select statement
               $sqlselect = "SELECT dueID, basic, penalty, sef, idle, tdID, dueDate,
                                    currentdate,paidBasic,paidSEF,paidPenalty, paidIdle,
                                    paymentMode, paidQuarters
                             FROM dues where tdID = '$tdID' and dueDate = '$dateDue'";
			   //query the database
               $queryID = $rptsDB->query($sqlselect);
               if($rptsDB->next_record()){
                   $this->dueID = $rptsDB->f("dueID");
                   $this->basic      = $rptsDB->f("basic");
                   $this->penalty    = $rptsDB->f("penalty");
                   $this->sef        = $rptsDB->f("sef");
                   $this->idle        = $rptsDB->f("idle");
                   $this->tdID      = $rptsDB->f("tdID");
				   $this->dueDate     = $rptsDB->f("dueDate");
                   $this->updateDate     = $rptsDB->f("currentDate");
                   $this->paidBasic   = $rptsDB->f("paidBasic");
                   $this->paidPenalty = $rptsDB->f("paidPenalty");
                   $this->paidSEF     = $rptsDB->f("paidSEF");
                   $this->paidIdle     = $rptsDB->f("paidIdle");				 
                   $this->paymentMode = $rptsDB->f("paymentMode");
                   $this->paidQuarters = $rptsDB->f("paidQuarters");
                   $this->amnesty = $rptsDB->f("amnesty");
                   # taxable year is date format and current date is dateTime format
				   /*$validDate = strtotime($taxableYear);
                   if($validDate > -1){
                       $this->dueDate=$validDate;
                   }
                   $validDate = strtotime($rptsDB->f("currentDate"));
                   if($validDate > -1){
                       $this->updateDate=$validDate;
                   }*/
                   $this->isStoredInDatabase = true;
				   return true;
               }
               else{
                   $this->setTdID($tdID);
                   return false;
               }
         }
     }
?>